import time from 'locutus/php/datetime/time';
import indexOf from "lodash/indexOf";
import each from "lodash/each";

const {action} = jinghuan.props;

/**
 * 维修项目
 */
class project extends jinghuan.AdminController {


    /**
     *
     * @return {QueryBuilder}
     * @private
     */
    _listModel(id = 0) {
        let model = this.db('jh_service_project')
            .where('status', '>', 0);

        if (id) {
            model.where('id', id).first()
        }

        return model;
    }


    /**
     *
     */
    @action({auth: false, login: false})
    async datatable() {
        let model1 = this._listModel();

        let model2 = this.db('jh_service_project')
            .where('status', '>', 0)
            .count('id as count');

        await this.default_datatable2(model1, model2, [])
    }

    @action({auth: false, login: false})
    async get_type() {
        let data = await this.db('jh_service_project')
            .select('id as value', 'name as title')

        this.callback(data);
    }

    @action({auth: false, login: false})
    async get_item(get, post) {
        let data = await this.db('jh_pro_fitting as a')
            .leftJoin('jh_fitting as b', 'b.id', 'a.fitting_id')
            .where('a.status', 1)
            .where('a.pro_id', post.id)
            .select('b.id', 'a.number', 'b.code', 'b.name', 'b.unit', 'b.origin', 'b.model', 'a.id as f_id');

        let arr = [];
        data.forEach((item, index) => {
            item.tag = 2;
            arr[item.id] = item;
        })
        this.callback(arr);
    }

    @action({})
    async get_itemlist(get, {code}) {
        let model = this.db('jh_fitting').where('status', 1).select('id', 'name', 'code', 'origin', 'model', 'unit');
        if (code) {
            model.where('code', 'like', `%${code}%`);
        }

        let data = await model;
        let arr = [];
        data.forEach((item, index) => {
            item.tag = 1;
            arr.push(item);
        })
        this.callback(arr);
    }

    @action({})
    async do_delete_item(get, post) {
        let res = await this.db('jh_pro_fitting').where('id', post.id).update({status: 0, user_id: 0, u_time: time()});
        this.callback(res);
    }

    @action({})
    async do_set_item(get, {id, items, number}) {
        if (items && number) {
            let fitting = items.split(',');
            let num = number.split(',');
            let arr = [];
            fitting.forEach((n, k) => {
                let one = {
                    pro_id: id,
                    fitting_id: n,
                    number: num[k],
                    status: 1,
                    shop_id: 0,
                    user_id: 0,
                    time: time(),
                    u_time: time()
                }
                arr.push(one);
            });
            console.log(arr)
            let res = await this.db('jh_pro_fitting').insert(arr);
            console.log(res)
            this.callback(res);
        }

    }

    @action({})
    async get_fitting(get, post) {
        this.callback(post);
    }

    @action({auth: false})
    async do_add({}, {name, code, desc, work}) {
        try {
            let model = await this.db('jh_service_project')
                .insert({
                    name,
                    code,
                    desc,
                    work,
                    shop_id: 0,
                    user_id: 0,
                    time: time(),
                    u_time: time()
                });
            this.record('添加工时成功');
            let data = await this._listModel(model[0]);
            this.callback(data, {toast: "message.success.add"});
        } catch (e) {
            this.record('添加工时失败');
            if (e.errno == 1062) {
                this.callback(false, {toast: 'message.error.codes'});
            } else {
                this.callback(false, {toast: 'message.error.add'});
            }
        }
    }


    @action({auth: false})
    async do_edit(GET, {name, code, id, desc, work}) {
        try {
            let model = await this.db('jh_service_project')
                .where('id', '=', id)
                .update({
                    name,
                    code,
                    desc,
                    work,
                    user_id: 0,
                    u_time: time()
                });
            this.record('编辑工时成功');
            let data = await this._listModel(id);
            this.callback(data, {toast: "message.success.edit"});
        } catch (e) {
            this.record('编辑工时失败');
            if (e.errno == 1062) {
                this.callback(false, {toast: 'message.error.code'});
            } else {
                this.callback(false, {toast: 'message.error.edit'});
            }
        }
    }

}

export default project;